package com.zyh.system.supermark1.dao.impl;

import com.zyh.system.supermark1.dao.GoodsDao;
import com.zyh.system.supermark1.db.BaseDAO;
import com.zyh.system.supermark1.domain.Goods;

import java.util.List;

public class GoodsDaoImpl implements GoodsDao {

    // 查询展示所有商品
    @Override
    public void selectAllGoods() {
        String sql = " select * from goods";
        List<Goods> goodsList = BaseDAO.selectData(sql, Goods.class);
        System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t商品库存");
        for (Goods goods : goodsList) {
            System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getStock());
        }
    }

    // 根据指定的商品 id 查询
    @Override
    public Goods selectByGoodsId(String goodsId) {
        String sql = "select * from goods where id=?";
        List<Goods> goods = BaseDAO.selectData(sql, Goods.class, goodsId);
        if (goods.size() != 0) {
            return goods.get(0);
        }
        return null;
    }

    // 添加商品信息
    @Override
    public int addGoods(Goods goods) {
        String sql = "insert into goods value(?,?,?,?)";
        return BaseDAO.changeOneData(sql, goods.getId(), goods.getName(), goods.getPrice(), goods.getStock());
    }

    // 更新商品库存信息
    @Override
    public int updateGoodsStockInfo(String id, Integer updateStock) {
        String sql = "update goods set stock=? where id=?";
        return BaseDAO.changeOneData(sql, updateStock, id);
    }

    // 修改商品信息
    @Override
    public int updateGoodsInfo(Goods goods) {
        String sql = "update goods set name=?,price=?,stock=? where id=?";
        return BaseDAO.changeOneData(sql, goods.getName(), goods.getPrice(), goods.getStock(),goods.getId());
    }
}
